package com.hongji.lms5xxcommunicator.opc.bootstrap;

import com.hongji.lms5xxcommunicator.realtime.WsTransferService;
import com.hongji.lms5xxcommunicator.grid.RasterGrid;
import com.hongji.lms5xxcommunicator.grid.config.RasterGridHolder;
import com.hongji.lms5xxcommunicator.grid.core.GridSpec;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RequiredArgsConstructor
public class GridSpecPublisher {

    private final RasterGridHolder rasterGridHolder;
    private final WsTransferService wsTransfer;

    public void publishOnce() {
        try {
            RasterGrid grid = rasterGridHolder.get();
            GridSpec spec = grid.spec();

            Map<String, Object> gridSpecDto = new HashMap<>();
            Map<String, Object> gridSpecMap = new HashMap<>();
            gridSpecMap.put("x0", spec.x0); gridSpecMap.put("x1", spec.x1);
            gridSpecMap.put("y0", spec.y0); gridSpecMap.put("y1", spec.y1);
            gridSpecMap.put("dx", spec.dx); gridSpecMap.put("dy", spec.dy);
            gridSpecDto.put("gridSpec", gridSpecMap);

            // 用新的 WS 工具推送
            wsTransfer.pushDto(gridSpecDto);

            log.info("已发送网格规格信息给前端: x[{}, {}], y[{}, {}], dx={}, dy={}",
                    spec.x0, spec.x1, spec.y0, spec.y1, spec.dx, spec.dy);
        } catch (Exception e) {
            log.error("发送网格规格信息失败", e);
        }
    }
}
